$(function(){
	var timer, index = 1, galleryLength = $('.gallery a').length;
	
	function show(_index){
		if(arguments.length == 1){
			index = _index;
		}

		$('.gallery a').eq(index).fadeIn(500);
		$('.gallery a').eq(index).siblings().fadeOut(500);
		
		$('.circle ul li').eq(index).find('img').attr('src','images/D-16.png');
		$('.circle ul li').eq(index).siblings().find('img').attr('src','images/D-15.png');
			
		index ++;
		if(index == galleryLength){
			index = 0;
		}
	};
	
	function flipPic(){
		timer = window.setInterval(show,6000);
	};
	
	flipPic();
	
	$('.circle ul li').click(function(){
		var _index = $(this).index();
		
		show(_index);
		window.clearInterval(timer);
		timer = window.setInterval(show,6000);
	});
});